//
//  AVClient.m
//  Pink
//
//  Created by ZhongDaYunKe on 15/12/4.
//  Copyright © 2015年 ycy. All rights reserved.
//

#import "AVClient.h"

@implementation AVClient


+(void)saveObjectWithClassName:(NSString *)className model:(NSObject *)model success:(void (^)(BOOL succeeded, NSError *error))success
{
    
    NSDictionary *modelDic = [model modelToJSONObject];
    AVObject *object = [AVObject objectWithClassName:className dictionary:modelDic];
    [object saveInBackgroundWithBlock:success];
    
}


+(void)getObjectWithClassName:(NSString *)className objectId:(NSString *)objectId modelType:(modelType)type success:(void (^)(id ob))success failed:(void (^)(NSError *error))failer
{
    AVQuery *query = [AVQuery queryWithClassName:className];
   [query getObjectInBackgroundWithId:objectId block:^(AVObject *object, NSError *error) {
       
       if (object) {
           BaseModel *convertObj = [[BaseModel alloc] init];
           NSMutableDictionary *objDic = [object dictionaryForObject];
           [objDic setObject:object.createdAt forKey:@"createdAt"];
           [objDic setObject:object.updatedAt forKey:@"updatedAt"];
           switch (type) {
               case UserTopicType:
                   convertObj = [UserTopic modelWithJSON:objDic];
                   break;
               case NewsType:
                   convertObj = [News modelWithJSON:objDic];
                   break;
                   
               default:
                   break;
           }
           success(convertObj);
           
       }else{
           failer(error);
           
       }
       
   }];


}

+ (NSMutableDictionary *)RealDictionaryForObject:(AVObject *)object
{
    NSMutableDictionary *objDic = [object dictionaryForObject];
    [objDic setObject:object.createdAt forKey:@"createdAt"];
    [objDic setObject:object.updatedAt forKey:@"updatedAt"];
    return objDic;
    
}


@end
